<?php
 include "counter2.php";
 requirelogin();

 # Print list of possible values for "usage" in email
 function usagelist($name, $selection) {
       $somethingselected = 0;
       print '<select name="' . $name . '">';
       $usagelist = array("main", "old", "spare", "delete");
       # HACK. Need the following line to get translations for the previous
       # line. The variable isn't used anywhere !!
       $trans_usagelist = array(_("main"), _("public"), _("old"), _("spare"), _("delete"));
       # End HACK

       foreach ($usagelist as $usage) {
          if ($selection == $usage) {
	     print "<option value=\"$usage\" selected>" . _($usage) . "\n";
	     $somethingselected = 1;
	  } else {
	     print "<option value=\"$usage\">" . _($usage) . "\n";
          }
       }
       if (!$somethingselected) {
          print "<option value=\"$selection\" selected>" . _($selection) . "\n";
       }
       print "</select>";

 }

 iamashowpage();


?>
<?php pagestart();
print "<html>\n";
pagehead(_("Email address edit"));
?>
<BODY bgcolor="white">
<?php pagetop(_("Email address edit")) ?>

<?php showmessage() ?>

<!-- Main content goes here -->
<?php
print _("Note that this function is still somewhat experimental - please report problems to the help address") ?>:
<?php print emailCloaking("help@counter.li.org") ?>
<p>
<?php
   $userid = currentuser();
   $query = mysql_query("select f_key, state, email.usage, level,
                        error, may_publish
                        from email
			where owner = $userid");
   if (!$query) {
      print ("MySQL error: " . mysql_errno() . ":" . mysql_error()
      . "<br>");
      die("Query for email addresses failed");
   }

?>
<form method=POST action="/cgi-bin/update-emails.cgi">
<table>
<?php print "<tr>\n<th>" . _("Email") . "</th>\n<th>" . _("Pub") . "</th>\n<th>" . _("Usage") . "</th>\n<th colspan=3>" . _("Error, if any") ?>
<?php
   while ($arr = mysql_fetch_array($query)) {
       print "<tr>\n<td>";
       if ($arr[usage] == "main") {
          print "<b>";
       }
       print "<font size=+1>" . emailCloaking($arr["f_key"], 0) . "</font>";
       if ($arr[usage] == "main") {
          print "</b>";
       }
       $pubname = "\"pub-" . $arr[f_key] . "\"";
       if ($arr[may_publish] == "YES") {
         print "<td><input name=$pubname type=\"checkbox\" value=\"YES\" checked>";
       } else {
         print "<td><input name=$pubname type=\"checkbox\" value=\"YES\">";
       }
       print "</td>\n<td>";
       usagelist("usage-" . $arr[f_key], $arr[usage]);       
       if ($arr[state] != "ok") {
          print "</td>\n<td>";
          print $arr[state];
          print "</td>\n<td>";
          print $arr[level];
	  print "</td>\n<td>";
	  print $arr[error];

       }
       print "</td>\n</tr>";
   }

?>
<tr>
<td><?php print _("Add email") ?>:<br><input name="new-email" size=40
maxlength=256></td>
<td valign="bottom"><input name="new-may_publish" type="checkbox" value="YES"></td>
<td valign="bottom"><?php usagelist("new-usage", "spare") ?></td>
</tr>
</table>
<input type="submit">
<input type="reset">
</form>
<h2><?php print _("Help with email addresses") ?></h2>
<?php print _("You must have one \"main\" email - this is your contact with the counter, and MUST be valid.\n") ?><br>
<?php print _("\"Spare\" emails are emails that you can be found via, but are not listed anywhere on the public site. These must be valid. The counter staff may use one of these to contact you if your main email bounces.\n") ?><br>
<?php print _("\"Old\" emails are emails that you used to have, but no longer use. If someone else registers with that email, they are allowed to take it. (With all the other types, you own that email.)\n") ?><br>
<?php print _("To delete an email, change its usage to \"delete\" and hit \"submit\".\n") ?>
<p>
<?php print _("Email with a mark on the \"Pub\" checkbox will be public, and shown on your public page (if you have one). All others will never be shown to anyone, but they may be used to search for you.\n") ?>
<?php pagebottom() ?>
</body>
</html>
